﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SmartWindow
{
    class FileToImageIconConverter
    {
        private System.Windows.Media.ImageSource icon;
        private int mode;

        private string FilePath;
        private Bitmap BitmapSource;

        public System.Windows.Media.ImageSource Icon
        {
            get
            {
                if (icon != null) return icon;
                if (FilePath != null)
                {
                    System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(FilePath);
                    icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                              sysicon.Handle,
                              System.Windows.Int32Rect.Empty,
                              System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                }
                else if (BitmapSource != null)
                {
                    if (mode == 0)
                    {
                        icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                  Properties.Resources.soundImage.GetHbitmap(),
                                  IntPtr.Zero,
                                  System.Windows.Int32Rect.Empty,
                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                    else if (mode == 1)
                    {
                        icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                  Properties.Resources.closeImage.GetHbitmap(),
                                  IntPtr.Zero,
                                  System.Windows.Int32Rect.Empty,
                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                    else if (mode == 2)
                    {
                        icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                  Properties.Resources.settings.GetHbitmap(),
                                  IntPtr.Zero,
                                  System.Windows.Int32Rect.Empty,
                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                    else if (mode == 3)
                    {
                        icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                  Properties.Resources.keyboard.GetHbitmap(),
                                  IntPtr.Zero,
                                  System.Windows.Int32Rect.Empty,
                                  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                }
                return icon;
            }
        }

        public FileToImageIconConverter(string filePath)
        {
            FilePath = filePath;
        }

        public FileToImageIconConverter(Bitmap bitmap, int mode)
        {
            BitmapSource = bitmap;
            this.mode = mode;
        }

    }
}
